<template>
	<view>
		<picker :value="index" :range="array" mode="multiSelector" @columnchange="onChange" @change="selectValue">
			<view class="timeUp">
				<view style="display: flex;align-items: center;">
					<image style="width: 25px;height:25px;margin-right: 8px;" src="../../static/shangmen.png"></image>
					<view>
						<view style="display: flex;align-items: center;font-weight: bold;">
							<view style="white-space: nowrap;">上门时间：</view>
							<view>{{selectedValues.length>0?array[0][selectedValues[0]]+' '+array[1][selectedValues[1]]:'请选择上门时间'}}</view>
						</view>
						<view style="font-size: 12px;color: #989898;margin-top: 4px;">衣物送洗前请折叠并做好简易包装</view>
					</view>
				</view>
				<image style="width: 20px;margin: 0;height: 20px;" src="../../static/right.png"></image>
			</view>
		</picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				array:[],
				selectedValues:[],
				currentIndex:0
			}
		},
		created() {
			const days = this.getLastTenDays()
			this.array[0] = days
			this.getTimeInterval()
		},
		methods:{
			selectValue(e) {
				if(this.array[1].length <=0) return uni.showToast({
					title:'当前没有可用时间',
					icon:'none'
				})
				this.selectedValues = e.target.value
				let value = e.target.value 
				let day = this.array[0][value[0]]
				let times = this.array[1][value[1]]
				let dateString = ''
				if(day === '今天'){
					let date = new Date();
					date.setDate(date.getDate());
					dateString = date.toISOString().split('T')[0]; // Format as YYYY-MM-DD
				}
				let time = times.split('-')
				let returnData = {
					start: dateString?dateString+' '+time[0]+':00':day+' '+time[0]+':00',
					end: dateString?dateString+' '+time[1]+':00':day+' '+time[1]+':00',
				}
				this.$emit("getIntervalTimes", returnData);
			},
			onChange(e) {
				
				if(e.target.column === 0){
					this.currentIndex = e.detail.value;
					this.getTimeInterval()
				}
			},
			getTimeInterval () {
				let start = this.getCurrentTimes()
				if(this.currentIndex !== 0) start = 7
				let data = [
					[8,10],
					[10,12],
					[12,14],
					[14,16],
					[16,18],
					[18,20],
				]
				let newData = []
				for(let i=data.length-1;i>=0;i--){
					let item = data[i]
					if(item[0]<start) data.splice(i,1)
				}
				data.map(item=>{
					newData.push((item[0]<10?'0'+item[0]+':00-':item[0]+':00-')+item[1]+':00')
				})
				this.array[1] = newData
			},
			combineAdjacentNumbers(arr) {
			  return arr.reduce((result, current, index, array) => {
			    if (index + 1 < array.length) {
			      result.push([current, array[index + 1]]);
			    }
			    return result;
			  }, []);
			},
			getCurrentTimes() {
				let hours = new Date()
				hours.setHours(new Date().getHours()+1)
				return hours.getHours()
			},
			getLastTenDays() {
			  let dates = [];
			  for (let i = 0; i < 10; i++) {
			    let date = new Date();
			    date.setDate(date.getDate() + i);
			    let dateString = date.toISOString().split('T')[0]; // Format as YYYY-MM-DD
			    dates.push(dateString); // Push to beginning of array
			  }
			  dates[0] = '今天'
			  return dates;
			}
		}
	}
</script>

<style>
	.timeUp{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
</style>